package org.byqh.dqeai.util;

import com.easy.query.core.basic.jdbc.executor.internal.merge.result.StreamResultSet;
import com.easy.query.core.basic.jdbc.executor.internal.props.JdbcProperty;
import com.easy.query.core.basic.jdbc.types.EasyParameter;
import com.easy.query.core.basic.jdbc.types.handler.JdbcTypeHandler;
import lombok.SneakyThrows;

import java.nio.charset.StandardCharsets;
import java.sql.SQLException;

public class StringBlobHandler implements JdbcTypeHandler {

    @Override
    public Object getValue(JdbcProperty jdbcProperty, StreamResultSet streamResultSet) throws SQLException {
        var bytes = streamResultSet.getBytes(jdbcProperty.getJdbcIndex());

        return new String(bytes);
    }

    @SneakyThrows
    @Override
    public void setParameter(EasyParameter parameter) throws SQLException {
        String param = (String) parameter.getValue();
        byte[] bytes = null;

        if (param != null) {
            bytes = param.getBytes(StandardCharsets.UTF_8);
        }

        parameter.getPs().setBytes(parameter.getIndex(), bytes);
    }
}
